package com.gitee.grower.core.scheme

import com.gitee.grower.util.*
import taboolib.common5.Coerce
import taboolib.module.configuration.Configuration

class Scheme(val root: Configuration) {

    val id = root.file?.nameWithoutExtension!!

    private val option = root.getConfigurationSection("__option__")!!

    val match = option.getString("match")!!

    val structures = root.mapListAs("structure") { Structure(it) }

    class Structure(val root: Map<String, Any?>) {

        val index = root["in"].asListOrInt()

        val chance = root["chance"]?.toString() ?: "1"

        val gold = Coerce.toDouble(root["gold"])

        val isNegative = Coerce.toBoolean(root["negative-enable"])

        val isDegraded = Coerce.toBoolean(root["degraded"])

        val successAction = root["success-action"]?.toString() ?: ""
        val degradedAction = root["degraded-action"]?.toString() ?: ""
        val degradedPerfectAction = root["degraded-perfect-action"]?.toString() ?: ""
        val failAction = root["fail-action"]?.toString() ?: ""
        val negativeAction = root["negative-action"]?.toString() ?: ""
        val negativePerfectAction = root["negative-perfect-action"]?.toString() ?: ""

        val degradedChance = root["degraded-chance"]?.toString() ?: "1"

        val negativeChance = root["negative-chance"]?.toString() ?: "0"

        val negativeLore = root["negative-lore"].asListOrString()

        val items = root["item"].asMap().map {

            val value = it.value

            if (value is LinkedHashMap<*,*>) {
                Item.Condition(Configuration.fromMap(value))
            } else {
                Item.Default(it.key,value?.toString() ?: "")
            }
        }

        val attributes = root["attribute"].asMap().map {
            Attribute(it.key, it.value?.toString() ?: "")
        }

    }

    class Attribute(val keyword: String, val valueScript: String)

}